﻿using Flurl.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FilterIllegalCharacter.UI
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        private List<string> accountList = new List<string>();
        public Login()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var localAccountList = DictFileHelper.ReadFile("account.txt");
            accountList.AddRange(localAccountList);
            string remoteUrl = ConfigurationHelper.AppSettings.RemoteAccountUrl;
            if (!string.IsNullOrEmpty(remoteUrl))
            {
                try
                {
                    var remoteAccounts = remoteUrl.GetStringAsync().Result;
                    var remoteAccountList = remoteAccounts.Split("\r\n").ToList();
                    accountList.AddRange(remoteAccountList);
                }
                catch
                {
                    if (MessageBox.Show("网络连接异常") == MessageBoxResult.OK)
                    {
                        this.DialogResult = false;
                    }
                }
            }
            textBoxAccount.Focus();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string account = $"{textBoxAccount.Text}|{textBoxPassword.Password}";
            if (accountList.Contains(account))
            {
                this.DialogResult = true;
            }
            else 
            {
                MessageBox.Show("账号或者密码错误");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult= false;
        }
    }
}
